// pages/details/details.js
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        topBarHeight: 0,

        hasImage: false,
        imageHeight: 0,//图像高度

        content: {},

        comments: [],

        focus: false,
        textTyping:'',
        bottom_plate_height: 0,
        reply_first: null,//回复的回复的index
        reply_second: null,//三级评论（有@的）index

        placeholder: '发个评论吧...',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        let content = this.data.content
        let imageHeight = this.data.imageHeight

        console.log(options.content)
        content = JSON.parse(options.content)
        this.setData({
            content
        })
        console.log("comments")
        console.log(this.data.comments)
        if (content.wallPicUrls != undefined && content.wallPicUrls.length != 0) {
            wx.getImageInfo({
                src: content.wallPicUrls[0],
                success: function (res) {
                    that.setData({
                        hasImage: true,
                        imageHeight: 750 * res.height / res.width,
                    })
                },
                fail: function () {
                    console.error("fail to load a image" + content.wallPicUrls[0])
                }
            })
        }

        this.refreshComment()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.setData({
          topBarHeight: app.globalData.capsuleObj.bottom
        });
      },

    exit: function () {
        wx.navigateBack({
          delta: 1,
        })
    },

    onFocus: function (event) {
        if (app.globalData.userInfo == null) {
            wx.showToast({
                title: "请先登录",
                icon: "none"
            })
            this.setData({
                focus: false
            })
        }
        else 
            this.setData({
                bottom_plate_height : event.detail.height+1
            })
    },
    // 失去焦点
    onBlur: function () {
        this.setData({
            bottom_plate_height : 0,
            placeholder: '发个评论吧...',
            reply_first: null,
            reply_second: null,
        })
    },

    onInput: function (event) {
        this.setData({
            textTyping: event.detail.value,
            placeholder: "别害羞嘛..."
        })
    },
    // 点击空白处跳出@，不过其实应该加一个评论符号，点击符号
    onTapBlank: function (param) {
        if (this.data.focus)
            this.setData({
                focus: false,
            })
    },

    onSend: function () {
        let textTyping = this.data.textTyping
        if(textTyping.match(/^[ ]*$/)) {
            wx.showToast({
                title: "爱要说出来",
                icon: "none"
            })
            return
        }

        var requestData = {
            anno: false,
            articleId: this.data.content.wallId,
            content: this.data.textTyping,
        }
        console.log("first")
        console.log(this.data.reply_first)
        console.log(this.data.comments)
        if (this.data.reply_first != null)
            requestData.parentId = this.data.comments[this.data.reply_first].id
        if (this.data.reply_second != null) {
            requestData.toUid = this.data.comments[this.data.reply_first].children[this.data.reply_second].authorId
            requestData.content = "回复@" + this.data.comments[this.data.reply_first].children[this.data.reply_second].nickName + " : " + requestData.content
        }

        app.wxapi('POST', "/wall/comment/new", requestData, 'application/x-www-form-urlencoded', res => {
            if (res.success) {
                this.setData({
                    textTyping: '',
                    placeholder: '发个评论吧...'
                })
                this.refreshComment()
            } else {
                console.log(res)
            }
        }, (err) => {
            //调用服务端登录接口失败
            console.error(err.errMsg)
        })


    },

    refreshComment: function () {
        var requestData = {
            textId: this.data.content.wallId
        }

        app.wxapi('GET', "/wall/comment/getById", requestData, 'application/x-www-form-urlencoded', res => {
            console.log(res)
            if (res.success) {
                this.setData({
                    comments: res.data
                })
            } else {
                console.log(res)
            }
        }, (err) => {
            //调用服务端登录接口失败
            console.error(err.errMsg)
        })
    },

    onTapComment_first: function (param) {
        if (this.data.reply_first == null) {
            let index = param.currentTarget.dataset.comment_index
            this.setData({
                reply_first: index,
                placeholder: '回复@' + this.data.comments[index].nickName,
                focus: true,
            })
        }
        else 
            this.onTapBlank()
    },

    onTapComment_second: function (param) {
        console.log("second")
        console.log(param.currentTarget.dataset)
        console.log("index")
        console.log(param.currentTarget.dataset.comment_index)
        console.log(param.currentTarget.dataset.comment_subindex)
        if (this.data.reply_second == null) {
            let index = param.currentTarget.dataset.comment_index
            let subindex = param.currentTarget.dataset.comment_subindex
            this.setData({
                reply_first: index,
                reply_second: subindex,
                placeholder: '回复@' + this.data.comments[index].children[subindex].nickName,
                focus: true,
            })
        }
        else 
            this.onTapBlank()
    },
    // 点赞
    onSwitchLike: function (params) {
        if (app.globalData.userInfo == null) {
            wx.showToast({
                title: '请先登录',
                icon: 'error',
                duration: 2000
            })
            return;
        }
        let that = this
        let content = this.data.content

        var data = {
            textId: content.wallId,
            state: !content.wallliked
        }
        // 发送点赞或者取消点赞的请求
        //代表开始是没赞
        app.wxapi('POST', "/wall/thumb/updateLikeState", data, 'application/x-www-form-urlencoded', res => {
            if (res.success) {
                console.log("点赞成功");
                content.wallliked = !content.wallliked
                this.setData({
                    content
                })

                if (content.wallliked) {
                    content.wallThumbs++;
                    this.setData({
                        content
                    })
                } else {
                    content.wallThumbs--;
                    this.setData({
                        content
                    })
                }
            } else {
                console.log("没有回应");
                wx.showModal({
                    title: '提示',
                    content: '提交失败',
                });
            }
        }, (err) => {
            //调用服务端登录接口失败
            console.log(err.errMsg)
        })
    }

})